import * as Sequelize from "sequelize";
import { Application } from "egg";

interface FuncAttributes {
    id: string;
    parent_id: string;
    name: string;
    level: string;
    icon?: string;
    pattern?: string;
    status: string;
    remark: string;
    create_user_id: string;
    modify_user_id: string;
    create_time: string;
    modify_time: string;
}

type FuncInstance = Sequelize.Instance<FuncAttributes> & FuncAttributes;

export default (app: Application) => {
    const { STRING, CHAR, UUID, UUIDV4, DATE,NOW } = app.Sequelize;
    const attributes: SequelizeAttributes<FuncAttributes> = {
        id: { type: UUID, primaryKey: true, allowNull: false, defaultValue: UUIDV4 },
        parent_id: { type: UUID, allowNull: true,},
        name: { type: STRING(50), allowNull: false },
        level: { type: CHAR(3), allowNull: false, defaultValue: "000" },
        icon: { type: STRING(50), allowNull: true },
        pattern: { type: STRING(50), allowNull: true },
        status: { type: CHAR(3), allowNull: false, defaultValue: "000" },
        remark: { type: STRING(200), allowNull: true },
        create_user_id: { type: UUID, allowNull: false },
        modify_user_id: { type: UUID, allowNull: false },
        create_time: { type: DATE, allowNull: false ,defaultValue: NOW},
        modify_time: { type: DATE, allowNull: false ,defaultValue: NOW}
    };
    const model = app.model.define<FuncInstance, FuncAttributes>("Func", attributes,{
        tableName: 't_func'
    });
    model.associate = ()=>{
        app.model.models.Func.hasMany(app.model.models.RoleFunc,{foreignKey:"func_id", sourceKey:"id"})
    }
    return model
};